package com.iyjhsoc.game.util;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.iyjhsoc.game.dao.GameChooseRolesDAO;
import com.iyjhsoc.game.dao.GameRoleDAO;
import com.iyjhsoc.game.dao.PlayerDAO;
import com.iyjhsoc.game.dao.TeamDAO;
import com.iyjhsoc.game.domain.entity.*;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigDecimal;

@Component
public class GameFill {

    @Resource
    GameChooseRolesDAO chooseRolesDAO;
    @Resource
    GameRoleDAO gameRoleDAO;
    @Resource
    PlayerDAO playerDAO;
    @Resource
    TeamDAO teamDAO;

    public CompetitionSmallTeamInfoDO getFillCompetitionSmall(JSONObject team){
        CompetitionSmallTeamInfoDO smallTeamInfoDO = new CompetitionSmallTeamInfoDO();
        smallTeamInfoDO.setKillNum((Integer) team.get("death_num"));
        smallTeamInfoDO.setDeathNum((Integer) team.get("kill_num"));
        smallTeamInfoDO.setAssistsNum((Integer) team.get("assist_num"));
        smallTeamInfoDO.setGold((Integer) team.get("gold"));
        String team_name = (String) team.get("team_name");
        QueryWrapper<TeamDO> teamDOQueryWrapper = new QueryWrapper<>();
        teamDOQueryWrapper.eq("name",team_name);
        TeamDO teamDO = teamDAO.selectOne(teamDOQueryWrapper);
        smallTeamInfoDO.setTeamId(teamDO.getId());
        Boolean is_win = (Boolean)team.get("is_win");
        if(is_win){
            smallTeamInfoDO.setIsVictory(1);
        } else {
            smallTeamInfoDO.setIsVictory(0);
        }
        smallTeamInfoDO.setDefenseTower((Integer) team.get("push_tower_num"));
        smallTeamInfoDO.setTyrant((Integer) team.get("kill_tyrant_num"));
        smallTeamInfoDO.setDarkTyrant((Integer) team.get("kill_dark_tyrant_num"));
        smallTeamInfoDO.setDominate((Integer) team.get("kill_big_dragon_num"));
        smallTeamInfoDO.setStorm((Integer) team.get("kill_storm_dragon_king_num"));
        return smallTeamInfoDO;
    }



    public  GameChooseRolesDO gameChooseRoles(JSONObject battlePlayer,Long competitionSmallGameId){
        GameChooseRolesDO gameChooseRolesDO = new GameChooseRolesDO();
        String hero_name = (String) battlePlayer.get("hero_name");
        String player_name = (String) battlePlayer.get("player_name");
        QueryWrapper<PlayerDO> playerDOQueryWrapper = new QueryWrapper<>();
        playerDOQueryWrapper.eq("name",player_name);
        PlayerDO playerDO = playerDAO.selectOne(playerDOQueryWrapper);
        if(playerDO == null){
            playerDO = new PlayerDO();
            playerDO.setName(player_name);
            playerDAO.insert(playerDO);
        }
        QueryWrapper<GameRoleDO> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.eq("name",hero_name);
        GameRoleDO gameRoleDO = gameRoleDAO.selectOne(objectQueryWrapper);
        if(gameRoleDO == null){
            gameRoleDO = new GameRoleDO();
            gameRoleDO.setName(hero_name);
            gameRoleDO.setGameName("王者荣耀");
            gameRoleDAO.insert(gameRoleDO);
        }
        gameChooseRolesDO.setGameRoleId(gameRoleDO.getId());
        gameChooseRolesDO.setCompetitionSmallGameId(competitionSmallGameId);
        gameChooseRolesDO.setPlayerId(playerDO.getId());
        String team_name = (String) battlePlayer.get("team_name");
        QueryWrapper<TeamDO> teamDOQueryWrapper = new QueryWrapper<>();
        teamDOQueryWrapper.eq("name",team_name);
        TeamDO teamDO = teamDAO.selectOne(teamDOQueryWrapper);
        gameChooseRolesDO.setTeamId(teamDO.getId());
        Integer gold = (Integer) battlePlayer.get("gold");
        gameChooseRolesDO.setGold(gold);
        BigDecimal  hurt_to_hero_total_rate = new BigDecimal(String.valueOf( battlePlayer.get("hurt_to_hero_total_rate")));
        Integer hurt_to_hero_total = (Integer) battlePlayer.get("hurt_to_hero_total");
        gameChooseRolesDO.setOutputRate(hurt_to_hero_total_rate);
        gameChooseRolesDO.setHurt(hurt_to_hero_total);
        Integer is_mvp = (Integer) battlePlayer.get("is_mvp");
        gameChooseRolesDO.setIsMvp(is_mvp);
        BigDecimal kda = new BigDecimal(String.valueOf(battlePlayer.get("kda")));
        gameChooseRolesDO.setKda(kda);
        Integer kill_num = (Integer) battlePlayer.get("kill_num");
        gameChooseRolesDO.setKillNum(kill_num);
        BigDecimal participation_rate = new BigDecimal(String.valueOf(battlePlayer.get("participation_rate")));
        gameChooseRolesDO.setParticipateRate(participation_rate);
        BigDecimal bear_rate = new BigDecimal(String.valueOf(battlePlayer.get("be_hurt_by_hero_total_rate")));
        gameChooseRolesDO.setBearRate(bear_rate);
        Integer be_hurt_by_hero_total = (Integer) battlePlayer.get("be_hurt_by_hero_total");
        gameChooseRolesDO.setBear(be_hurt_by_hero_total);
        Integer death_num = (Integer) battlePlayer.get("death_num");
        Integer assists_num = (Integer) battlePlayer.get("assist_num");
        gameChooseRolesDO.setDeathNum(death_num);
        gameChooseRolesDO.setAssistsNum(assists_num);
        String position_desc = (String) battlePlayer.get("position_desc");
        gameChooseRolesDO.setPositionDesc(position_desc);
        return gameChooseRolesDO;
    }
}
